package org.example.service.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dao.dao.user.ULabelDao;
import org.example.dao.dao.user.UUserLabelDao;
import org.example.dao.dto.user.UserDto;
import org.example.dao.entity.user.ULabel;
import org.example.dao.entity.user.UUserLabel;
import org.example.exception.ParamLossErrorException;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.commom.IdWorker;
import org.example.utils.user.RedisLabelService;
import org.example.utils.user.RedisUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 22449
 */
@Service("labelService")
public class LabelServiceImpl {

    @Resource
    private IdWorker idWorker;

    @Resource
    private ULabelDao labelDao;

    @Resource
    private UUserLabelDao uUserLabelDao;

    @Resource
    private ContextUtil contextUtil;

    public void deleteLabel(Long id) {
        ULabel label = labelDao.selectById(id);
        if (label == null) {
            throw new ParamLossErrorException("id不存在");
        }
        label.setIsValid(0);
        label.setUpdateTime(new Date());
        labelDao.updateById(label);
    }

    public ULabel updateLabel(Long id, String name, String background) {
        ULabel label = labelDao.selectById(id);
        if (label == null) {
            throw new ParamLossErrorException("id不存在");
        }
        label.setName(name);
        label.setBackground(background);
        label.setUpdateTime(new Date());
        labelDao.updateById(label);
        return label;
    }

    @Resource
    private RedisLabelService redisLabelService;

    public IPage<ULabel> selectLabel(Integer limit, Integer start) {
        return redisLabelService.getLabelTotalList(start, limit);
    }

    public ULabel addLabel(String name, String background) {
        ULabel label = new ULabel(idWorker.nextId(), name, background);
        labelDao.insert(label);
        return label;
    }

    public void deleteUserLabel(Long id) {
        UUserLabel uUserLabel = uUserLabelDao.selectById(id);
        uUserLabel.setIsValid(0);
        uUserLabel.setUpdateTime(new Date());
        uUserLabelDao.updateById(uUserLabel);
        redisUserService.delUserDto(contextUtil.getId().toString());
    }

    @Resource
    RedisUserService redisUserService;

    public void addUserLabel(Long labelId) {
        UserDto userDto = redisUserService.getUserDto(contextUtil.getId().toString());
        //只能绑定五个标签
        if (userDto.getLabels().size() >= 5) {
            throw new ParamLossErrorException("只能绑定五个标签");
        }
        //不能绑定重复标签
        for (ULabel label : userDto.getLabels()) {
            if (label.getId().equals(labelId)) {
                throw new ParamLossErrorException("不能绑定重复标签");
            }
        }
        UUserLabel uUserLabel = new UUserLabel(idWorker.nextId(), contextUtil.getId(), labelId);
        uUserLabelDao.insert(uUserLabel);
        redisUserService.delUserDto(userDto.getUser().getId().toString());
    }

    public void updateUserLabel(List<String> list) {
        if (list.size() >= 5) {
            throw new ParamLossErrorException("只能绑定四个标签");
        }
        Long userId = contextUtil.getId();
        LambdaQueryWrapper<UUserLabel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UUserLabel::getUserId, userId);
        //已经绑定了的
        List<UUserLabel> labels = uUserLabelDao.selectList(lambdaQueryWrapper);
        for (UUserLabel uUserLabel : labels) {
            Long labelId = uUserLabel.getLabelId();

            int index = list.indexOf(labelId.toString());

            if (index == -1) {
                //需要绑定的列表 不包含 直接失效
                uUserLabel.setIsValid(0);
                uUserLabel.setUpdateTime(new Date());
                uUserLabelDao.updateById(uUserLabel);
            } else {
                //需要绑定的列表 包含 在需要绑定的列表中删除该部分
                list.remove(index);
            }

        }
        Set<String> set = new HashSet<>(list);
        for (String labelId : set) {
            UUserLabel uUserLabel = new UUserLabel(idWorker.nextId(), contextUtil.getId(), Long.valueOf(labelId));
            uUserLabelDao.insert(uUserLabel);
        }

        redisUserService.delUserDto(userId.toString());
    }
}
